//
//  MTMapVC.m
//  met
//
//  Created by 李晓俊 on 2019/6/24.
//  Copyright © 2019 李晓俊. All rights reserved.
//

#import "MTMapVC.h"
#import "MTMapCollectionViewCell.h"

@interface MTMapVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@end

@implementation MTMapVC
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self getInfo];
}
- (void)getInfo {
    //    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    AccountService *acc = [AccountService getObject];
    acc.httpRequestResultBlock = ^(id data,int code){
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (code == 1) {
            [self.dataArr removeAllObjects];
            
            self.dataArr = [NSMutableArray arrayWithArray:data];
            
            [self.collectionView reloadData];
        }else {
            [self.view maketishiToast:data];
        }
    };
    [acc mapFind];
}
- (NSMutableArray *)dataArr{
    if (_dataArr == nil) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
- (void)initUI{
    self.navigationItem.title = @"地图";
    
    [self.view addSubview:self.collectionView];
    self.collectionView.frame = self.view.bounds;
}
#pragma mark - colloectionviewdelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MTMapCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"mapCell" forIndexPath:indexPath];
    cell.model = [self.dataArr objectAtIndex:indexPath.row];
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    //    return CGSizeMake((kScreenWidth - 40 - 30) / 4, 45);
    return CGSizeMake(64, 145);
    
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 15;
}
// 该方法是设置一个section的上左下右边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(15, 15, 15, 15);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    MapModel *model  = [self.dataArr objectAtIndex:indexPath.row];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    AccountService *acc = [AccountService getObject];
    acc.httpRequestResultBlock = ^(id data,int code){
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (code == 1) {
            [self.view maketishiToast:@"修改成功"];
            NSMutableDictionary *dic = [NSMutableDictionary new];
            dic[@"mapUrl"] = [NSString stringWithFormat:@"%@",model.mapUrl];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:MTMapNotification object:nil userInfo:dic];

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else {
            [self.view maketishiToast:data];
        }
    };
    [acc updUsersMapInfo:[NSString stringWithFormat:@"%@",model.mapId]];
}


#pragma mark - get
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 15;
        layout.minimumInteritemSpacing = 15;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = WhiteColor;
        //        _collectionView.bounces = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        [_collectionView registerClass:[MTMapCollectionViewCell class] forCellWithReuseIdentifier:@"mapCell"];
        
        //        [_collectionView registerClass:[MTCardListHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"cardHeader"];
    }
    return _collectionView;
}
@end
